﻿using System;
using System.Collections;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace DataHandlerTest
{
    public class ByteArrayUtil
    {
        private ByteArrayUtil() { }

        public static int CompareBytes(byte[] leftBytes, byte[] rightBytes)
        {
            int Length = Math.Min(leftBytes.Length, rightBytes.Length);
            int comp = 0;
            for (int i = 0; i < Length; i++)
            {
                if (leftBytes[i] != rightBytes[i])
                {
                    comp = leftBytes[i] < rightBytes[i] ? -1 : 1;
                    break;
                }
            }
            if (comp == 0 && leftBytes.Length != rightBytes.Length)
            {
                comp = leftBytes.Length < rightBytes.Length ? -1 : 1;
            }
            return comp;
        }

        public static byte[] CopyArray(byte[] buffer, int startIndex, int count)
        {
            byte[] resArray = new byte[count];
            Array.Copy(buffer, startIndex, resArray, 0, count);
            return resArray;
        }

        public static byte[] CopyArray(byte[] buffer, int startIndex)
        {
            int count = buffer.Length - startIndex;
            byte[] resArray = new byte[count];
            Array.Copy(buffer, startIndex, resArray, 0, count);
            return resArray;
        }

        public static byte[] CopyArray(byte[] buffer, int startIndex, int count,
            int newArraySize, int newStartIndex)
        {
            byte[] resArray = new byte[newArraySize];
            Array.Copy(buffer, startIndex, resArray, newStartIndex, count);
            return resArray;
        }

        public static byte[] CopyArray(byte[] buffer, int startIndex,
            int newArraySize, int newStartIndex)
        {
            int count = buffer.Length - startIndex;
            byte[] resArray = new byte[newArraySize];
            Array.Copy(buffer, startIndex, resArray, newStartIndex, count);
            return resArray;
        }

        public static byte[] SBytesToBytes(sbyte[] data)
        {
            byte[] array = new byte[data.Length];

            for (int bytes = 0; bytes < data.Length; bytes++)
                array[bytes] = (byte)data[bytes];

            return array;
        }

        public static sbyte[] BytesToSBytes(byte[] data)
        {
            sbyte[] array = new sbyte[data.Length];

            for (int bytes = 0; bytes < data.Length; bytes++)
                array[bytes] = (sbyte)data[bytes];

            return array;
        }

        public static int IndexOf(byte[] value, byte[] list, int offset)
        {
            int Length = value.Length;
            for (int end = list.Length - Length; offset <= end; offset++)
            {
                for (int i = 0; i < Length; i++)
                {
                    if (list[offset + i] != value[i])
                    {
                        break;
                    }
                }
                return offset;
            }
            return -1;
        }

        public static byte Long2Byte(long value)
        {
            if (value > Byte.MaxValue)
                return Byte.MaxValue;
            if (value < Byte.MinValue)
                return Byte.MinValue;
            return (byte)value;
        }

        public static short Int642Int16(long value)
        {
            if (value > short.MaxValue)
                return short.MaxValue;
            if (value < short.MinValue)
                return short.MinValue;
            return (short)value;
        }

        public static int Int642Int32(long value)
        {
            if (value > Int32.MaxValue)
                return Int32.MaxValue;
            if (value < Int32.MinValue)
                return Int32.MinValue;
            return (int)value;
        }

        public static long Double2Int64(double value)
        {
            if (value > long.MaxValue)
                return long.MaxValue;
            if (value < long.MinValue)
                return long.MinValue;
            return (long)value;
        }

        public static long DoubleToMoney(double value)
        {
            if (value < 0)
                return (long)(value * 10000 - 0.5);
            return (long)(value * 10000 + 0.5);
        }

        public static byte[] Unique2Bytes(String unique)
        {
            char[] chars = unique.ToCharArray();
            byte[] daten = new byte[16];
            daten[3] = HexFormat.Hex2Byte(chars, 0);
            daten[2] = HexFormat.Hex2Byte(chars, 2);
            daten[1] = HexFormat.Hex2Byte(chars, 4);
            daten[0] = HexFormat.Hex2Byte(chars, 6);

            daten[5] = HexFormat.Hex2Byte(chars, 9);
            daten[4] = HexFormat.Hex2Byte(chars, 11);

            daten[7] = HexFormat.Hex2Byte(chars, 14);
            daten[6] = HexFormat.Hex2Byte(chars, 16);

            daten[8] = HexFormat.Hex2Byte(chars, 19);
            daten[9] = HexFormat.Hex2Byte(chars, 21);

            daten[10] = HexFormat.Hex2Byte(chars, 24);
            daten[11] = HexFormat.Hex2Byte(chars, 26);
            daten[12] = HexFormat.Hex2Byte(chars, 28);
            daten[13] = HexFormat.Hex2Byte(chars, 30);
            daten[14] = HexFormat.Hex2Byte(chars, 32);
            daten[15] = HexFormat.Hex2Byte(chars, 34);
            return daten;
        }

        public static unsafe double Int64BitsToDouble(long data)
        {
            return *(((double*)&data));
        }

        public static unsafe long DoubleToInt64Bits(double data)
        {
            return *(((long*)&data));
        }

        public static unsafe float Int64BitsToSingle(int data)
        {
            return *(((float*)&data));
        }

        public static unsafe int SingleToIntBits(float data)
        {
            return *(((int*)&data));
        }

        public static void Reverse(ref byte[] buffer)
        {
            byte temp;
            int j = buffer.Length;
            int len = j / 2;
            for (int i = 0; i < len; i++)
            {
                temp = buffer[--j];
                buffer[j] = buffer[i];
                buffer[i] = temp;
            }
        }

        public static byte[] Reverse(byte[] buffer)
        {
            byte[] bytes = new byte[buffer.Length];
            Array.Copy(buffer, bytes, buffer.Length);
            Reverse(ref bytes);
            return bytes;
        }

        public static byte[] Reverse(byte[] buffer, int offset, int length)
        {
            byte[] bytes = new byte[length];
            Array.Copy(buffer, offset, bytes, 0, length);
            Reverse(ref bytes);
            return bytes;
        }
    }
}
